<?php
/**
 * 
 * 打工 处理类
 * @author tanzhe
 *
 */
class WorkModel {
	private $db;
	private $cache;
	private $workRequire;
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
		global $workRequire;
		$this->workRequire = $workRequire;
	}
	
	public function getJob($key, $workPlace) { //获得工作列表信息
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		//判断是否满足进入条件 
		if ( isset($this->workRequire[$workPlace]) ) {
			if ( $this->workRequire[$workPlace]['hanger'] > $playerInfo['hanger'] ) {
				return array('code'=>206,'ret'=>'');
			}
		} else {
			return array('code'=>209,'ret'=>'');
		}
		$work = $this->cache->get('worklist_'.$workPlace);
		if ( empty($work) ) {
			$query = $this->db->query("select `label` from work where `workplace`='{$workPlace}' order by id asc");
			$data = $this->db->fetchAllByQuery($query);
			$work = array();
			foreach ( $data as $v ) {
				$work[] = $v['label'];
			}
			$this->cache->set('worklist_'.$workPlace, $work);
		}
		return array('code'=>0,'ret'=>array('work'=>$work));
	}

	/**
	 * 
	 * 打工
	 * @param str $key
	 * @param str $label 工作标识
	 */
	function doJob($key, $label) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( !empty($label) ) {
			$workData = $this->cache->get('workdata_'.$label);
			if ( empty($workData) ) {
				$query = $this->db->query("select * from work where `label`='{$label}'");
				$workData = $this->db->fetchOneByQuery($query);
				$this->cache->set('workdata_'.$label, $workData);
			}
			if ( empty($workData) ) { //不存在该工作
				return array('code'=>209,'ret'=>'');
			}
		} else {
			return array('code'=>209,'ret'=>'');
		}
		
		//判断衣架条件
		if ( isset($this->workRequire[$workData['workplace']]) ) {
			if ( $this->workRequire[$workData['workplace']]['hanger'] > $playerInfo['hanger'] ) {
				return array('code'=>206,'ret'=>'');
			}
		} else {
			return array('code'=>209,'ret'=>'');
		}
		
		//判断体力值
		if ( $workData['use_energy'] > $playerInfo['now_energy'] ) {
			return array('code'=>210,'ret'=>'');
		}
		//判断工作属性
		if ( $workData['work_hotness'] > $playerInfo['work_hotness'] ) {
			return array('code'=>211,'ret'=>'');
		}
		//更新数据库与缓存
		$updateData = array();
		//能量满时消耗掉后开始恢复
		//$countDown = 0;
		if ( $playerInfo['now_energy'] == $playerInfo['max_energy'] ) {
			$time = time();
			$updateData['last_energy_time'] = $time;
			global $fillEnergyCdTime;
			$countDown = floor($fillEnergyCdTime/(1+$playerInfo['cd_factor']/100));
			$updateData['next_energy_time'] = $time + $countDown;
		} 
		//获得游戏币数量 暂为考虑加成情况
		$earn = $workData['earn'];
		if ( !empty($playerInfo['coin_factor']) ) {
			$earn += $playerInfo['coin_factor'];
		}
		$updateData['coin'] = $playerInfo['coin'] + $earn;
		$updateData['now_energy'] = $playerInfo['now_energy'] - $workData['use_energy'];
		//工作熟练度
		if ( isset($playerInfo['job'][$workData['workplace']][$label]) ) {
			$playerInfo['job'][$workData['workplace']][$label] += 1;
		} else {
			$playerInfo['job'][$workData['workplace']][$label] = 1;
		}
		//判断是否获得元宝
		$cashCondition = $playerInfo['job'][$workData['workplace']][$label] / $workData['experience'];
		if ( ($cashCondition == 1) OR ($cashCondition == 2) OR ($cashCondition == 3) ) {
			$getCash = 1;
			$updateData['cash'] = $playerInfo['cash'] + 1;
		}
		$playerInfo = array_merge($playerInfo, $updateData);
		//位置不可换
		$updateData['job'] = serialize($playerInfo['job']);
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( isset($getCash) ) {
			$aryData = array(
				'label'=>$label,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'dojob',
				'num'=> 1,
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		//返回个人信息
		$ret = array(
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
			'now_energy'=>$playerInfo['now_energy'],
			'max_energy'=>$playerInfo['max_energy'],
			'count_down'=>$playerInfo['next_energy_time'] - time(),
			'exp'=>$playerInfo['job'][$workData['workplace']][$label],
		);
		return array('code'=>0,'ret'=>$ret);
	}
}
?>